﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Framework.Library.Core.Utils.BBCodeParse
{
    public class BBCodeUtils
    {
        public static string GetParser(string bbCode, ErrorMode errorMode)
        {
            return GetParser(bbCode, errorMode, BBTagClosingStyle.AutoCloseElement, false);
        }

        public static string GetParser(string bbCode, ErrorMode errorMode, BBTagClosingStyle listItemBbTagClosingStyle, bool enableIterationElementBehavior)
        {
            return GetParser(errorMode, true, listItemBbTagClosingStyle, enableIterationElementBehavior).ToHtml(bbCode).Replace("\r", "").Replace("\n", "<br/>");
        }

        private static BBCodeParser GetParser(ErrorMode errorMode, bool includePlaceholder, BBTagClosingStyle listItemBBTagClosingStyle, bool enableIterationElementBehavior)
        {
            return new BBCodeParser(errorMode, null, new[]
                {
                    new BBTag("b", "<b>", "</b>"), 
                    new BBTag("i", "<span style=\"font-style:italic;\">", "</span>"), 
                    new BBTag("u", "<span style=\"text-decoration:underline;\">", "</span>"), 
                    new BBTag("code", "<pre class=\"prettyprint\">", "</pre>"), 
                    new BBTag("img", "<img src=\"${content}\" />", "", false, true), 
                    new BBTag("center", "<div style=\"text-align: center;\">", "</div>"),
                    new BBTag("left", "<div style=\"text-align: left;\">", "</div>"), 
                    new BBTag("right", "<div style=\"text-align: right;\">", "</div>"), 
                    new BBTag("justify", "<div style=\"text-align: justify;\">", "</div>"), 
                    new BBTag("quote", "<blockquote>", "</blockquote>"), 
                    new BBTag("list", "<ul>", "</ul>"), 
                    new BBTag("*", "<li>", "</li>", true, listItemBBTagClosingStyle, null, enableIterationElementBehavior), 
                    new BBTag("url", "<a href=\"${href}\">", "</a>", new BBAttribute("href", ""), new BBAttribute("href", "href")),
                    new BBTag("url2", "<a href=\"${href}\">", "</a>", new BBAttribute("href", "", GetUrl2Href), new BBAttribute("href", "href", GetUrl2Href)),
                    new BBTag("size", "<font size=\"${size}\">", "</font>", new BBAttribute("size", ""), new BBAttribute("size", "size")), 
                    new BBTag("color", "<font color=\"${color}\">", "</font>", new BBAttribute("color", ""), new BBAttribute("color", "color")), 
                    new BBTag("font", "<font face=\"${face}\">", "</font>", new BBAttribute("face", ""), new BBAttribute("face", "face")), 
                    !includePlaceholder ? null : new BBTag("placeholder", "${name}", "", false, BBTagClosingStyle.LeafElementWithoutContent, null, new BBAttribute("name", "", name => "xxx" + name.AttributeValue + "yyy")), 
                }.Where(x => x != null).ToArray());
        }
        static string GetUrl2Href(IAttributeRenderingContext attributeRenderingContext)
        {
            if (!string.IsNullOrEmpty(attributeRenderingContext.AttributeValue)) return attributeRenderingContext.AttributeValue;

            var content = attributeRenderingContext.GetAttributeValueByID(BBTag.ContentPlaceholderName);
            if (!string.IsNullOrEmpty(content) && content.StartsWith("http:")) return content;

            return null;
        }
    }
}
